package com.aura.common.core.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;

import java.util.List;

/**
 * 字符串工具类 支持通配符，如 *、**、?
 */
public class StringUtil {

    private static final AntPathMatcher MATCHER = new AntPathMatcher();

    private StringUtil() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    /**
     * 判断指定字符串是否匹配 pattern 列表中的任意一个
     *
     * @param str      待匹配字符串
     * @param patterns 匹配集合列表
     * @return 是否匹配
     */
    public static boolean matchesAnyPattern(String str, List<String> patterns) {
        if (StringUtils.isEmpty(str) || CollectionUtils.isEmpty(patterns)) {
            return false;
        }
        for (String pattern : patterns) {
            if (isMatch(pattern, str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断指定 url 是否符合 pattern 匹配规则
     *
     * @param pattern 匹配规则
     * @param url     待匹配的路径字符串
     * @return 是否匹配
     */
    public static boolean isMatch(String pattern, String url) {
        return MATCHER.match(pattern, url);
    }
}
